// 初始化目标 MongoDB 实例
print('Initializing MongoDB Target Instance...');

// 等待数据库启动
sleep(5000);

// 初始化副本集
try {
  rs.initiate({
    _id: "rs0",
    members: [
      { _id: 0, host: "localhost:27017" }
    ]
  });
  print('Replica set initialized successfully for target instance');
} catch (error) {
  print('Replica set already initialized or error:', error.message);
}

// 等待副本集选举完成
sleep(3000);

// 创建测试数据库和集合
db = db.getSiblingDB('testdb');

// 创建用户集合
db.createCollection('users');
db.users.createIndex({ "email": 1 }, { unique: true });

// 创建产品集合
db.createCollection('products');
db.products.createIndex({ "sku": 1 }, { unique: true });

// 创建订单集合
db.createCollection('orders');
db.orders.createIndex({ "order_number": 1 }, { unique: true });

print('Target instance initialization completed!');
print('Created collections: users, products, orders');
print('Database: testdb');
print('Connection: localhost:20728');
print('Note: This instance is ready to receive synced data from source'); 